package com.block.datapush.special.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.exception.NotifyExceptionService;
import com.block.datapush.model.dto.MerchantCountDTO;
import com.block.datapush.special.service.ISpecialCostomCountInfoService;
import com.block.db.entity.SpecialCostomCountInfo;
import com.block.db.mapper.SpecialCostomCountInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-08-30
 */
@Service
public class SpecialCostomCountInfoServiceImpl extends ServiceImpl<SpecialCostomCountInfoMapper, SpecialCostomCountInfo> implements ISpecialCostomCountInfoService {


    @Autowired
    NotifyExceptionService notifyExceptionService;

    @Async
    @Override
    @Transactional
    public void saveCountInfo(MerchantCountDTO merchantCountDTO){
        try {
            merchantCountDTO.setDateInfo(LocalDate.now().toString());

            SpecialCostomCountInfo dataCountInfo = this.baseMapper.selectByDateInfoAndInfoId(merchantCountDTO.getInfoId(), merchantCountDTO.getDateInfo());
            if (ObjectUtil.isEmpty(dataCountInfo)){
                dataCountInfo = this.initCountInfo(merchantCountDTO);
                if (ObjectUtil.isEmpty(dataCountInfo)){
                    throw new RuntimeException(StrUtil.format("新增线索统计信息异常【sourceCountDTO】{}", JSON.toJSONString(dataCountInfo)));
                }
            }

            SpecialCostomCountInfo updateInfo = this.getUpdateEntity(merchantCountDTO, dataCountInfo);
            updateInfo.setId(dataCountInfo.getId());
            boolean update = this.updateById(updateInfo);
            if (!update){
                throw new RuntimeException(StrUtil.format("修改线索统计信息异常【sourceCountDTO】{}", JSON.toJSONString(merchantCountDTO)));
            }

        }catch (Exception exception){
            exception.printStackTrace();
            notifyExceptionService.notifyNoticeInfo("SOURCE_COUNT_EXC", "线索统计异常", StrUtil.format("【sourceCountDTO】{}", JSON.toJSONString(merchantCountDTO)), exception);
        }
    }

    private SpecialCostomCountInfo getUpdateEntity(MerchantCountDTO merchantCountDTO, SpecialCostomCountInfo specialCostomCountInfo) {
        SpecialCostomCountInfo updateEntity = new SpecialCostomCountInfo();

        if (ObjectUtil.isNotEmpty(merchantCountDTO.getCheckNumber())){
            updateEntity.setCheckNumber(merchantCountDTO.getCheckNumber() + specialCostomCountInfo.getCheckNumber());
        }

        if (ObjectUtil.isNotEmpty(merchantCountDTO.getCheckPassNumber())){
            updateEntity.setCheckPassNumber(merchantCountDTO.getCheckPassNumber() + specialCostomCountInfo.getCheckPassNumber());
        }

        if (ObjectUtil.isNotEmpty(merchantCountDTO.getRegisterNumber())){
            updateEntity.setRegisterNumber(merchantCountDTO.getRegisterNumber() + specialCostomCountInfo.getRegisterNumber());
        }


        if (ObjectUtil.isNotEmpty(merchantCountDTO.getRegisterPassNumber())){
            updateEntity.setRegisterPassNumber(merchantCountDTO.getRegisterPassNumber() + specialCostomCountInfo.getRegisterPassNumber());
        }

        if (ObjectUtil.isNotEmpty(merchantCountDTO.getSettleNumber())){
            updateEntity.setSettleNumber(merchantCountDTO.getSettleNumber() + specialCostomCountInfo.getSettleNumber());
        }

        if (ObjectUtil.isNotEmpty(merchantCountDTO.getSettleAmount())){
            updateEntity.setSettleAmount(NumberUtil.add(merchantCountDTO.getSettleAmount() , specialCostomCountInfo.getSettleAmount()));
        }

        return updateEntity;
    }

    public SpecialCostomCountInfo initCountInfo(MerchantCountDTO countDTO){
        SpecialCostomCountInfo countInfo = new SpecialCostomCountInfo();
        countInfo.setInfoId(countDTO.getInfoId());
        countInfo.setInfoName(countDTO.getInfoName());
        countInfo.setDateInfo(countDTO.getDateInfo());

        boolean save = this.save(countInfo);
        if (!save){
            return null;
        }
        return this.getById(countInfo.getId());
    }


}
